package com.hey.jvm.v22;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;

public class RuntimeTest {

    public static void main(String[] args) {

        Properties props = System.getProperties();
        System.setProperty("amos", "This is a Test");
        for (Map.Entry<Object, Object> map : props.entrySet()){
            Object key = map.getKey();
            Object val = map.getValue();
            System.out.println(String.format("%s : %s", key, val));
        }


        Runtime runtime = Runtime.getRuntime();

        // Test availableProcessors()
        System.out.println("Available processors: " + runtime.availableProcessors());

        // Test totalMemory(), freeMemory(), maxMemory()
        System.out.println("Total memory: " + runtime.totalMemory());
        System.out.println("Free memory: " + runtime.freeMemory());
        System.out.println("Max memory: " + runtime.maxMemory());

        // Test gc() - Note: This is just a suggestion to JVM, actual GC may or may not happen.
        System.out.println("Invoking garbage collection...");
        runtime.gc();
        System.out.println("Free memory after GC: " + runtime.freeMemory());

        // Test exec() - Running 'echo Hello from Runtime' as an example command.
        try {
            Process process = runtime.exec("echo Hello from Runtime");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
            runtime.exit(110);
        }

        runtime.exit(000);
        // Note: loadLibrary() is typically used when you have native libraries to load.
        // Since it requires an actual library file and may vary across platforms, it's not demonstrated here.
    }
}

